/**
 * 
 */
package com.cap.bts.framework.workbench.userlogin.controller;

import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cap.bts.framework.auth.util.SSOConfigurations;
import com.cap.bts.framework.auth.util.UserAuthenticationUtils;
import com.cap.bts.framework.auth.util.WebUtils;
import com.cap.bts.framework.workbench.constant.WorkbenchConstant;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cap.bts.commonfunc.common.constant.OperationType;
import com.cap.bts.framework.bizlog.domain.BizLog;
import com.cap.bts.framework.bizlog.service.IBizLoggerService;
import com.cap.bts.framework.common.util.TokenUtils;

/**
 * @author Marshal
 * 
 */
@Controller
@RequestMapping(value = "/logout")
public class LogoutRestController {

    private static Logger logger = Logger.getLogger(LogoutRestController.class);
    
    @Autowired
    private IBizLoggerService bizloggerService;

    @RequestMapping
    public void logout(
        HttpServletRequest req, HttpServletResponse res, Model model) throws ServletException {
    	boolean ssoflog = false;
    	if(req.getSession(true).getAttribute("IS_SSO_LOGIN")!=null){
    		
    		String ssologinflag = (String)req.getSession(true).getAttribute("IS_SSO_LOGIN");
    		if("Y".equals(ssologinflag)){
    			ssoflog = true;
    		}
    	}
        //if (TokenUtils.verifyEncryptionToken(user, code)) {
            //invalid the cookie
            UserAuthenticationUtils.setLogout(req, res, "");
        //} 
        
        try {
            //redirect to the login page
            /*req.getSession().removeAttribute(WorkbenchConstant.PREVIOUS_URL);
            req.getSession().removeAttribute(WorkbenchConstant.URI_STRING);
            req.getSession().removeAttribute(WorkbenchConstant.QUERY_STRING);*/
        	
        	//Logging User Operation to BizLog
            /*BizLog userLog = new BizLog();
            userLog.setUserName(user);
            userLog.setOperationType(OperationType.LOGOUT_OPERATION.getValue());
            userLog.setOperationDesc(user + " " + OperationType.LOGOUT_OPERATION.getValue());
            userLog.setOperationTime(new Date());
            bizloggerService.log(userLog);   */     	
            
        	if(ssoflog){
        		String url = SSOConfigurations.getConfig("dealer.portal.url");
        		res.sendRedirect(url);
        	}else{
        		String loginUrlSuffix = WorkbenchConstant.LOGIN_PAGE_URL;
        		res.sendRedirect(req.getContextPath()+loginUrlSuffix);
        	}
//            String loginUrlSuffix = WorkbenchConstant.LOGIN_PAGE_URL;
//            WebUtils.redirectToLoginByURL(req, res, loginUrlSuffix);
        } catch (Exception e) {
        	e.printStackTrace();
            logger.error(e.getMessage());
        }
    }
}
